package com.school.generator.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: zhangyaoting
 * @date: 2019/07/03 22:00
 * @version: V1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class ColumnItem {
    /** 列名（中文） **/
    private String columnComment;
    /** 列名（英文） **/
    private String columnName;
    /** 属性 **/
    private String propertyName;
    /** 数据类型 **/
    private String jdbcType;
    /** java数据类型 **/
    private String javaType;
    /** 实体类数据类型 **/
    private String entityType;
    /** 键类型 **/
    private String columnKey;
    /** 是否可空 **/
    private String nullable;

    public void init(){
        String[] types = typeMap.get(jdbcType);
        if(types != null){
            jdbcType = types[0];
            javaType = types[1];
            entityType = types[2];
        }
        propertyName = camelCaseName(columnName);
    }

    /**
     * 转换为驼峰
     *
     * @param underscoreName
     * @return
     */
    public static String camelCaseName(String underscoreName) {
        StringBuilder result = new StringBuilder();
        if (underscoreName != null && underscoreName.length() > 0) {
            boolean flag = false;
            for (int i = 0; i < underscoreName.length(); i++) {
                char ch = underscoreName.charAt(i);
                if ("_".charAt(0) == ch) {
                    flag = true;
                } else {
                    if (flag) {
                        result.append(Character.toUpperCase(ch));
                        flag = false;
                    } else {
                        result.append(ch);
                    }
                }
            }
        }
        return result.toString();
    }

    private static Map<String, String[]> typeMap = new HashMap();

    static {
        typeMap.put("char", new String[]{"CHAR", "java.lang.String", "String"});
        typeMap.put("varchar", new String[]{"VARCHAR", "java.lang.String", "String"});
        typeMap.put("mediumtext", new String[]{"VARCHAR", "java.lang.String", "String"});
        typeMap.put("text", new String[]{"VARCHAR", "java.lang.String", "String"});
        typeMap.put("longvarchar", new String[]{"LONGVARCHAR", "java.lang.String", "String"});
        typeMap.put("numeric", new String[]{"NUMERIC", "java.lang.Double", "Double"});
        typeMap.put("decimal", new String[]{"DECIMAL", "java.lang.Double", "Double"});
        typeMap.put("bit", new String[]{"BIT", "java.lang.Boolean", "Boolean"});
        typeMap.put("boolean", new String[]{"BOOLEAN", "java.lang.Boolean", "Boolean"});
        typeMap.put("tinyint", new String[]{"TINYINT", "java.lang.Integer", "Integer"});
        typeMap.put("smallint", new String[]{"SMALLINT", "java.lang.Short", "Short"});
        typeMap.put("integer", new String[]{"INTEGER", "java.lang.Integer", "Integer"});
        typeMap.put("int", new String[]{"INTEGER", "java.lang.Integer", "Integer"});
        typeMap.put("bigint", new String[]{"BIGINT", "java.lang.Long", "Long"});
        typeMap.put("real", new String[]{"REAL", "java.lang.Float", "Float"});
        typeMap.put("float", new String[]{"FLOAT", "java.lang.Double", "Double"});
        typeMap.put("double", new String[]{"DOUBLE", "java.lang.Double", "Double"});
        typeMap.put("binary", new String[]{"BINARY", "java.lang.Byte[]", "Byte[]"});
        typeMap.put("varbinary", new String[]{"VARBINARY", "java.lang.Byte[]", "Byte[]"});
        typeMap.put("longvarbinary", new String[]{"LONGVARBINARY", "java.lang.Byte[]", "Byte[]"});
        typeMap.put("date", new String[]{"DATE", "java.util.Date", "Date"});
        typeMap.put("time", new String[]{"TIME", "java.util.Date", "Date"});
        typeMap.put("datetime", new String[]{"TIMESTAMP", "java.util.Date", "Date"});
        typeMap.put("timestamp", new String[]{"TIMESTAMP", "java.util.Date", "Date"});
        typeMap.put("clob", new String[]{"CLOB", "java.sql.Clob", "Clob"});
        typeMap.put("blob", new String[]{"BLOB", "java.sql.Blob", "Blob"});
    }
}
